package groep3.ssc.skaterViews;

import groep3.ssc.controllers.ModelRepository;
import groep3.ssc.helpers.DataFunctions;
import groep3.ssc.main.R;
import groep3.ssc.model.Race;
import groep3.ssc.model.Scheme;
import groep3.ssc.model.SchemeEntry;
import android.content.Context;
import android.content.res.Resources;
import android.widget.ArrayAdapter;

public class TargetDifferenceView extends SkaterViewTemplate
{
    private SkaterTextView tvDifference;

    public TargetDifferenceView(Context context)
    {
        super(context);
        hasSettings = true;
        tvDifference = new SkaterTextView(context);
        addView(tvDifference);
        subscribeToController();
    }

    @Override
    public void update(Race model)
    {
        Scheme s = model.getTargetScheme();
        if (s == null)
        {
            tvDifference.setText(getResources().getString(R.string.errorNoTargetSchemeSelected));
        } else
        {
            int currentLap = model.getSkater(model.getOurSkater()).getRoundNumber();
            SchemeEntry se = s.getLap(currentLap - 1);
            if (se == null)
            {
                tvDifference.setText(getResources().getString(R.string.errorTargetSchemeFinished));
            } else
            {
                long difference = model.getSkater(model.getOurSkater()).getTotalTime() - se.getTotalTime();
                tvDifference.setText(s.getAbbreviation() + ": " + DataFunctions.millisecondsToReadableTime(difference));
            }
        }
    }

    @Override
    public ArrayAdapter<Object> getSettingsAdapter(Context context)
    {
        ModelRepository models = ModelRepository.getInstance();
        ArrayAdapter<Object> adapter = new ArrayAdapter<Object>(context, android.R.layout.simple_list_item_1);
        adapter.setDropDownViewResource(android.R.layout.simple_list_item_1);
        adapter.addAll(models.getListOfSchemes());
        return adapter;
    }

    @Override
    public Object getSettingsSelectedItem()
    {
        ModelRepository models = ModelRepository.getInstance();
        if (models.getActiveRace() == null)
            return 0;
        return models.getActiveRace().getTargetScheme();
    }

    @Override
    public String toString()
    {
        Resources res = getResources();
        String differenceWithTargetSchemeString = res.getString(R.string.txtDifferenceWithTargetSchemeString);
        return differenceWithTargetSchemeString;
    }
}